package com.pan.analyzer;

import com.pan.util.PrintUtil;
import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class AnsjAnalyzer implements Analyzer,AnalyzerHelper{


    @Override
    public Map<String, Integer> getWordCount(List<String> sentences) {

        List<String> smallSentences = preAnalyze(sentences);

        List<String> allWords = new ArrayList<>();
        for (String small : smallSentences) {
            //
            Result parse = ToAnalysis.parse(small);
            List<Term> terms = parse.getTerms();
            for (Term term : terms) {
                //
                if(StringUtils.isBlank(term.getName())){
                    continue;
                }
                allWords.add(term.getName());
            }
        }

        Map<String,Integer> word_count_map = new HashMap<>(2048);
        for (String word : allWords) {
            //
            if(word_count_map.containsKey(word)){
                Integer integer = word_count_map.get(word);
                word_count_map.put(word,++integer);
            }else{
                word_count_map.put(word,1);
            }
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<>(word_count_map.entrySet());

        Collections.sort(list,new Comparator<Map.Entry<String,Integer>>() {
            //升序排序
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

    list.forEach(
        x -> {
          String key = x.getKey();
          Integer value = x.getValue();
          System.out.println(key+"-->"+value);
        });
        return word_count_map;
    }

    @Override
    public List<String> preAnalyze(List<String> sentences) {

      List<String> preHandleSentences = spliteSentencesByPunctuation(sentences);

        return preHandleSentences;
    }

    private List<String> spliteSentencesByPunctuation(List<String> sentences) {
        Iterator<String> iterator = sentences.iterator();
        List<String> preHandleSentences = new ArrayList<>();
        while (iterator.hasNext()){
            String sentence = iterator.next();
            List<String> smallSentences = spliteSentenceToSmall(sentence);
            preHandleSentences.addAll(smallSentences);
        }
        return preHandleSentences;
    }

    //半角空格与全角空格
    public static String[] punctuations = new String[]{"。","？","！","，","：","；","、"," ","　","“","”","（","）"};


    private List<String> spliteSentenceToSmall(String sentence) {

        List<String> smallSentences = new ArrayList<>();
        smallSentences.add(sentence);

        for (String punctuation : punctuations) {
            //根据标点符号进行分割
            smallSentences = spliteByPunctuation(smallSentences, punctuation);
        }

        return smallSentences;
    }

    private List<String> spliteByPunctuation(List<String> sentences,String punctuation){
        List<String> splitSentences = new ArrayList<>();
        Iterator<String> iterator = sentences.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            if(StringUtils.isBlank(next)){
                continue;
            }
            String cleanNext = next.trim();
            String[] split = cleanNext.split(punctuation);
            splitSentences.addAll(Arrays.asList(split));
        }
        return splitSentences;
    }


    public static void main(String[] args) {


        //
        String str = "健全目录清单、编制备案、衔接协调等规划管理制度，制定“十四五”国家级专项规划等目录清单，依托国家规划综合管理信息平台推进规划备案，将各类规划纳入统一管理。建立健全规划衔接协调机制，报请党中央、国务院批准的规划及省级发展规划报批前须与本规划进行衔接，确保国家级空间规划、专项规划、区域规划等各级各类规划与本规划在主要目标、发展方向、总体布局、重大政策、重大工程、风险防控等方面协调一致。" ;
        AnsjAnalyzer ansjAnalyzer = new AnsjAnalyzer();
        List<String> smalls = ansjAnalyzer.preAnalyze(Arrays.asList(str));
        for (String small : smalls) {
            //
            Result parse = ToAnalysis.parse(small);
            List<Term> strings = parse.getTerms();
            PrintUtil.printObjectList(strings);
        }
        Result parse = ToAnalysis.parse("健全");
        List<Term> strings = parse.getTerms();
        //      AnsjAnalyzer ansjAnalyzer = new AnsjAnalyzer();
        //      List<String> strings = ansjAnalyzer.preAnalyze(Arrays.asList(str));
        strings.forEach(
                x -> {
                    System.out.println(x.getName());

                });
    }


}
